<template>
  <up-popup :show="show" mode="bottom" :round="10" @close="handleCancel">
		<view class='popup'>
      <div class="popup-header">
        <view class="popup-title">编辑用户信息</view>
      </div>
      <scroll-view class="popup-content" scroll-y>
        <up-form class="p-[30rpx]">
          <up-form-item label="昵称">
            <up-input v-model="formData.nickname" placeholder="请输入昵称"></up-input>
          </up-form-item>
          <up-form-item label="等级">
            <f-data-picker v-model:value="formData.level_id" :columns="levelList" placeholder="请选择等级"></f-data-picker>
          </up-form-item>
          <up-form-item label="进度">
            <f-data-picker v-model:value="formData.jieduan" :columns="jieduanList" placeholder="请选择阶段"></f-data-picker>
          </up-form-item>
          <up-form-item label="手机">
            <up-input v-model="formData.mobile" placeholder="请输入手机号"></up-input>
          </up-form-item>
          <up-form-item label="门店">
            <f-store-picker v-model:value="formData.store_id"></f-store-picker>
          </up-form-item>
          <up-form-item label="备注">
            <up-input v-model="formData.beizhu" placeholder="请输入备注"></up-input>
          </up-form-item>
          <up-form-item label="积分">
            <up-input v-model="formData.jifen" placeholder="请输入修改后的值"></up-input>
          </up-form-item>
          <up-form-item label="生日">
            <f-date-picker v-model:value="formData.birthday" placeholder="请选择生日"></f-date-picker>
          </up-form-item>
          <up-form-item label="状态">
            <f-data-picker v-model:value="formData.status" :columns="statusList" placeholder="请选择状态"></f-data-picker>
          </up-form-item>
        </up-form>
      </scroll-view>
      <div class="popup-footer">
        <up-button type="primary" block plain @click="handleCancel">取 消</up-button>
        <up-button class="ml-[30rpx]" type="primary" block @click="handleConfirm">确 定</up-button>
      </div>
		</view>
	</up-popup>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import FStorePicker from '@/addon/htscrm/components/f-store-picker/index.vue'
import FDatePicker from '@/addon/htscrm/components/f-date-picker/index.vue'
import FDataPicker from '@/addon/htscrm/components/f-data-picker/index.vue'
import { getCustomerDataApi } from '@/addon/htscrm/api'

const emit = defineEmits(['cancel', 'confirm'])

const show = ref(false)
const formData = ref<any>({})
const levelList = ref<any[]>([])
const jieduanList = ref<any[]>([])
const statusList = ref<any[]>([
  {
    id: 1,
    name: '正常'
  },
  {
    id: 2,
    name: '待审核'
  },
  {
    id: 3,
    name: '已拒绝'
  },
  {
    id: -1,
    name: '已拉黑'
  }
])
function close () {
	show.value = false
}
function open (item:any) {
  show.value = true
  formData.value = JSON.parse(JSON.stringify(item))
  getCustomerData()
}
async function getCustomerData() {
  const res = await getCustomerDataApi({})
  if (res.code === 1) {
    // 等级选项
    levelList.value = res.data.levelArr.map((item:any, index:number) => {
      return {
        name: item,
        id: res.data.levelidArr[index]
      }
    })
    // 阶段选项
    jieduanList.value = res.data.jieduanArr.map((item:any, index:number) => {
      return {
        name: item,
        id: res.data.jieduanidArr[index]
      }
    })
  }
}

function handleCancel() {
  close()
  emit('cancel')
}

function handleConfirm() {
  close()
  emit('confirm', {
    user_id: formData.value.id,
    birthday: formData.value.birthday||'',
    nickname: formData.value.nickname||'',
    mobile: formData.value.mobile||'',
    beizhu: formData.value.beizhu||'',
    jieduan: formData.value.jieduan||0,
    levelId: formData.value.level_id,
    status: formData.value.status,
    store_id: formData.value.store_id,
    jifen: formData.value.jifen
  })
}


defineExpose({
  open,
  close
})
</script>

<style scoped lang="scss">
.popup {

	.popup-header {
    height: 100rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    // box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);

    .popup-title {
      font-size: 32rpx;
      font-weight: 500;
    }
	}

	.popup-content {
    height: 60vh;
	}

  .popup-footer {
    display: flex;
    padding: 20rpx 30rpx;
    box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);

  }
}
</style>